package com.night.encryption.util;

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class RSACryptography {


    /**
     * 参考 http://blog.csdn.net/qq_18870023/article/details/52596808
     *
     * @param args
     */
    public static void main(String[] args) {

        String data = "你好啊，world";

        try {
            KeyPair keyPair = genKeyPair(1024);
            //获取公钥，并以base64格式打印出来
            PublicKey publicKey = keyPair.getPublic();
            System.out.println("公钥：" + new String(Base64.getEncoder().encode(publicKey.getEncoded())));

            //获取私钥，并以base64格式打印出来
            PrivateKey privateKey = keyPair.getPrivate();
            System.out.println("私钥：" + new String(Base64.getEncoder().encode(privateKey.getEncoded())));

            //公钥加密
            byte[] encryptedBytes = encrypt(data.getBytes(), publicKey);
            System.out.println("加密后：" + new String(encryptedBytes));

            //私钥解密
            byte[] decryptedBytes = decrypt(encryptedBytes, privateKey);
            System.out.println("解密后：" + new String(decryptedBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //生成密钥对
    private static KeyPair genKeyPair(int keyLength) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        return keyPairGenerator.generateKeyPair();
    }

    //公钥加密
    private static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(content);
    }

    //私钥解密
    private static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(content);
    }
}
